﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using FacebookWrapper.ObjectModel;

namespace B12_Ex03_Eran_053094363_Assaf_036722999
{
    /// <summary>
    /// Implemnting Strategy Pattern
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class FacebookListStrategy<T>
    {     
        private List<T> m_FBReturnList;
        private bool v_Done = false;

        public Func<IEnumerable<T>> FacebookListFunc { get; set; }

        public void GenerateList(out List<T> o_OutputList)
        {
            Thread runThread = new Thread(runFunc);
            
            // Start the Thread
            runThread.Start();

            //Wait until The Thread is Done
            while (!v_Done)
            {
                Thread.Sleep(5000);
            }

            // Set the Output list as the Retun List
            o_OutputList = m_FBReturnList;
        }

        private void runFunc()
        {
            if (FacebookListFunc != null)
            {
                m_FBReturnList = FacebookListFunc.Invoke().ToList();
                v_Done = true;
            }
            else
            {
                throw new Exception("Facebook List Function is Empty!");
            }
        }
    }
}
